package com.hc.daoimp;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.hc.dao.RentDao;
import com.hc.entity.House;
import com.hc.entity.Rent;
import com.hc.entity.Tenant;
import com.hc.util.JDBCUtil;

public class RentDaoImpl implements RentDao {
	//查租户
	@Override
	public List<Tenant> getallteant() {
		ArrayList<Tenant> arrayList = new ArrayList<>();
		JDBCUtil.sql="select * from tenant";
		JDBCUtil.ps=JDBCUtil.getPs();
		try {
			JDBCUtil.rs=JDBCUtil.ps.executeQuery();
			while(JDBCUtil.rs.next()){
				Tenant tenant = new Tenant();
				int id = JDBCUtil.rs.getInt(1);
				String name = JDBCUtil.rs.getString(2);
				int phone = JDBCUtil.rs.getInt(3);
				String gender = JDBCUtil.rs.getString(4);
				String address = JDBCUtil.rs.getString(5);
				String number = JDBCUtil.rs.getString(6);
				String date = JDBCUtil.rs.getString(7);
				
				tenant.setTid(id);
				tenant.setTname(name);
				tenant.setTphone(phone);
				tenant.setTgender(gender);
				tenant.setTaddress(address);
				tenant.setTnumber(number);
				tenant.setTaddtime(date);
				
				arrayList.add(tenant);
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return arrayList;
	}
	//查房源
	@Override
	public List<House> getallhouse() {
		ArrayList<House> arrayList = new ArrayList<>();
		JDBCUtil.sql="select * from house";
		JDBCUtil.ps=JDBCUtil.getPs();
		try {
			
			JDBCUtil.rs=JDBCUtil.ps.executeQuery();
			while(JDBCUtil.rs.next()){
				House house = new House();
				house.setHid(JDBCUtil.rs.getInt(1));
				house.setHaddress(JDBCUtil.rs.getString(2));
				house.setHcommunity(JDBCUtil.rs.getString(3));
				house.setHunit(JDBCUtil.rs.getInt(4));
				house.setHfloor(JDBCUtil.rs.getInt(5));
				house.setHnum(JDBCUtil.rs.getInt(6));
				house.setHarea(JDBCUtil.rs.getInt(7));
				house.setHtoward(JDBCUtil.rs.getString(8));
				house.setHdecorate(JDBCUtil.rs.getString(9));
				house.setHshaungqi(JDBCUtil.rs.getString(10));
				house.setHlimit(JDBCUtil.rs.getInt(11));
				house.setHfacilities(JDBCUtil.rs.getString(12));
				house.setHprice(JDBCUtil.rs.getInt(13));
				house.setHstate(JDBCUtil.rs.getString(14));
				house.setHmessage(JDBCUtil.rs.getString(15));
				house.setHaddtime(JDBCUtil.rs.getString(16));
				house.setHupdatetime(JDBCUtil.rs.getString(17));
				arrayList.add(house);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return arrayList;
		
	}
	//添加
	@Override
	public void addrent(Rent rent) {
		JDBCUtil.sql="INSERT INTO rent VALUES(NULL,?,?,?,?,?)";
		JDBCUtil.ps=JDBCUtil.getPs();
		try {
			JDBCUtil.ps.setInt(1, rent.getRcount());
			JDBCUtil.ps.setString(2, rent.getRdate());
			JDBCUtil.ps.setString(3, rent.getRnote());
			JDBCUtil.ps.setInt(4, rent.getHouse().getHid());
			JDBCUtil.ps.setInt(5, rent.getTenant().getTid());
			
			JDBCUtil.ps.executeUpdate();
			JDBCUtil.close();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	@Override
	public List<Rent> select(String name, String date ,int phone) {
		ArrayList<Rent> arrayList = new ArrayList<>();
		JDBCUtil.sql="SELECT * FROM rent,house,tenant WHERE rent.hid=house.hid AND rent.tid=tenant.tid "
				+ "AND rdate LIKE ? AND tname LIKE ? AND tphone LIKE ?";
		JDBCUtil.ps=JDBCUtil.getPs();
		try {
			JDBCUtil.ps.setString(1, "%"+date+"%");
			JDBCUtil.ps.setString(2, "%"+name+"%");
			JDBCUtil.ps.setString(3, "%"+phone+"%");
			JDBCUtil.rs=JDBCUtil.ps.executeQuery();
			while(JDBCUtil.rs.next()){
				Rent rent=null;
				int rid = JDBCUtil.rs.getInt(1);
				int rcount = JDBCUtil.rs.getInt(2);
				String rdate = JDBCUtil.rs.getString(3);
				String rnote = JDBCUtil.rs.getString(4);
				int hid = JDBCUtil.rs.getInt(5);
				int tid = JDBCUtil.rs.getInt(6);
				String hmessage = JDBCUtil.rs.getString(21);
				String tname = JDBCUtil.rs.getString(25);
				rent=new Rent(rid, rcount, rdate, rnote, new House(hid, null, null, 0, 0, 0, 0, null, null, null, 0, null, 0, null, hmessage, null, null),
						new Tenant(tid, tname, 0, null, null, null, null, 0));
				
				arrayList.add(rent);
				
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return arrayList;
	}
	@Override
	public Rent show(int id) {
		Rent rent=null;
		JDBCUtil.sql="select * from rent where rid=?";
		JDBCUtil.ps=JDBCUtil.getPs();
		try {
			JDBCUtil.ps.setInt(1, id);
			JDBCUtil.rs=JDBCUtil.ps.executeQuery();
			while (JDBCUtil.rs.next()) {
				int rid = JDBCUtil.rs.getInt(1);
				int rcount = JDBCUtil.rs.getInt(2);
				String rdate = JDBCUtil.rs.getString(3);
				String rnote = JDBCUtil.rs.getString(4);
				int hid = JDBCUtil.rs.getInt(5);
				int tid = JDBCUtil.rs.getInt(6);
				rent=new Rent(rid, rcount, rdate, rnote, new House(hid, null, null, 0, 0, 0, 0, null, null, null, 0, null, 0, null, null, null, null),
						new Tenant(tid, null, 0, null, null, null, null, 0));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rent;
	}
	@Override
	public void update(Rent rent) {
		JDBCUtil.sql="UPDATE rent SET rcount=?,rdate=?,rnote=?,hid=?,tid=? WHERE rid=?";
		JDBCUtil.ps=JDBCUtil.getPs();
		try {
			JDBCUtil.ps.setInt(1, rent.getRcount());
			JDBCUtil.ps.setString(2, rent.getRdate());
			JDBCUtil.ps.setString(3, rent.getRnote());
			JDBCUtil.ps.setInt(4, rent.getHouse().getHid());
			JDBCUtil.ps.setInt(5, rent.getTenant().getTid());
			JDBCUtil.ps.setInt(6, rent.getRid());
			JDBCUtil.ps.executeUpdate();
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	@Override
	public void del(int id) {
		JDBCUtil.sql="delete from rent where rid=?";
		JDBCUtil.ps=JDBCUtil.getPs();
		try {
			JDBCUtil.ps.setInt(1, id);
			JDBCUtil.ps.executeUpdate();
			
			JDBCUtil.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	

}
